package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    @DubboReference
    private BrandService brandService;

    @RequestMapping("toBrand")
    public String toBrand(){
        return "/admin/brand";
    }

    @RequestMapping("listBrand")
    @ResponseBody
    public Result listBrand (@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        try {
            PageResult pageResult = brandService.listBrand(pageNum, pageSize);
            return Result.OK().setData(pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
    @RequestMapping("addBrand")
    @ResponseBody
    public Result addBrand(TbBrand tbBrand){
        try {
            brandService.addBrand(tbBrand);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){

        try {
            TbBrand tbBrand = brandService.toUpdate(id);
            return Result.OK().setData(tbBrand);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
    @RequestMapping("updateBrand")
    @ResponseBody
    public Result updateBrand(TbBrand tbBrand){
        try {
            brandService.updateBrand(tbBrand);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
    @RequestMapping("delBrands")
    @ResponseBody
    public Result delBrands(@RequestParam(value = "ids") List<Long> ids){
        try {
            brandService.delBrands(ids);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
}
